package com.ruoyix.novel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.novel.domain.bo.NovelChapterContentBo;
import com.ruoyix.novel.domain.vo.NovelChapterContentVo;
import com.ruoyix.novel.domain.NovelChapterContent;
import com.ruoyix.novel.mapper.NovelChapterContentMapper;
import com.ruoyix.novel.service.INovelChapterContentService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 单个章节内容Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-05
 */
@RequiredArgsConstructor
@Service
public class NovelChapterContentServiceImpl implements INovelChapterContentService {

    private final NovelChapterContentMapper baseMapper;

    /**
     * 查询单个章节内容
     */
    @Override
    public NovelChapterContentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询单个章节内容列表
     */
    @Override
    public TableDataInfo<NovelChapterContentVo> queryPageList(NovelChapterContentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NovelChapterContent> lqw = buildQueryWrapper(bo);
        Page<NovelChapterContentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询单个章节内容列表
     */
    @Override
    public List<NovelChapterContentVo> queryList(NovelChapterContentBo bo) {
        LambdaQueryWrapper<NovelChapterContent> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NovelChapterContent> buildQueryWrapper(NovelChapterContentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NovelChapterContent> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOutlineId() != null, NovelChapterContent::getOutlineId, bo.getOutlineId());
        lqw.eq(StringUtils.isNotBlank(bo.getChapterNumber()), NovelChapterContent::getChapterNumber, bo.getChapterNumber());
        lqw.like(StringUtils.isNotBlank(bo.getTitle()), NovelChapterContent::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), NovelChapterContent::getContent, bo.getContent());
        lqw.eq(bo.getAiGenerated() != null, NovelChapterContent::getAiGenerated, bo.getAiGenerated());
        lqw.eq(StringUtils.isNotBlank(bo.getGeneratedBy()), NovelChapterContent::getGeneratedBy, bo.getGeneratedBy());
        return lqw;
    }

    /**
     * 新增单个章节内容
     */
    @Override
    public Boolean insertByBo(NovelChapterContentBo bo) {
        NovelChapterContent add = BeanUtil.toBean(bo, NovelChapterContent.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改单个章节内容
     */
    @Override
    public Boolean updateByBo(NovelChapterContentBo bo) {
        NovelChapterContent update = BeanUtil.toBean(bo, NovelChapterContent.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NovelChapterContent entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除单个章节内容
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
